
var height = 400;
height = $(window).height() -200;
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'business/tregister/list',
        datatype: "json",
        colModel: [
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '产品大类', name: 'productTypeName', index: 'product_type_name', width: 80 },
			{ label: '产品名称', name: 'productName', index: 'product_name', width: 80 },
			{ label: '申请人', name: 'userName', index: 'user_name', width: 80 },
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 },
			{ label: '企业名称', name: 'companyName', index: 'company_name', width: 80 },
			{ label: '需求说明', name: 'demandDescribe', index: 'demand_describe', width: 80 },
			{ label: '来源', name: 'source', index: 'source', width: 80 },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 ,formatter:function (cellValue) {
				return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(cellValue))
			}},
			{ label: '操作', name: '', index: '', width: 80,formatter:function (cellValue,options,row) {
				if(row.productTypeName == '贷款'){
					return '<a style="margin-right:15px;color:#337ab7" href="javascript:void(0)" onclick="vm.update(null,\''+row.id+'\')">查看</a>';
				}else{
					return "";
				}
			} },

        ],
		viewrecords: true,
        height: height,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });


	//日期指定控件
	laydate.render({
		elem: '#searchDate',//指定元素
		type: 'date',
		theme: '#4476A7',
		range: '~'
	});

});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		tRegister: {},
		q:{
			productTypeName:"",
			productName:"",
			date:""
		}
	},
	methods: {
		query: function () {
			vm.q.date = $("#searchDate").val();
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.tRegister = {};
		},
		update: function (event,id) {

			vm.showList = false;
            vm.title = "查看";
            
            vm.getInfo(id)
			vm.tRegister.createTime = dateFtt("yyyy-MM-dd hh:mm:ss",new Date(vm.tRegister. createTime))
		},
		saveOrUpdate: function (event) {
			var url = vm.tRegister.id == null ? "business/tregister/save" : "business/tregister/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.tRegister),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "business/tregister/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			ajax(baseURL +"business/tregister/info/"+id,{},"get",false,false).then(function (r) {
				vm.tRegister = r.tRegister;
			})

		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
				postData:vm.q
            }).trigger("reloadGrid");
		},
		exportExcel:function () {
			if(null != $("#searchDate").val()){
				vm.q.date = $("#searchDate").val();
			}
			excel(baseURL + "business/tregister/export",vm.q)
		}
	}
});